/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package javaapplication6.UI;

/**
 *
 * @author meydlo
 */


import javaapplication6.ControlLayer.SellControl;
import javaapplication6.DomainLayer.SellProduct;
public class SellUI {

    private SellControl sellControl;
    private Listener listener=new Listener();


    public SellUI(){
        sellControl=new SellControl();
    }
    public void printChoices()
    {
        System.out.println(
                "\n (1) create sellProduct"+
                "\n (2) print sellProduct info"+
                "\n (3) update sellProduct"+
                "\n (4) delete sellProduct"+
                "\n (5) print choices"+
                "\n (6) print sellProductList"+
                "\n (7) main menu"
                );
    }
    public void sellProductManager()
    {
        boolean exit=false;
        while(exit==false){
            printChoices();
            int number=listener.getLineInt();
            switch(number){
                case 1:{
                    createSellProduct();
                    break;
                }
                case 2:{
                    printSellProductInfo();
                    break;
                }
                case 3:{
                    updateSellProduct();
                    break;
                }
                case 4:{
                    deleteSellProduct();
                    break;
                }
                case 5:{
                    printChoices();
                    break;
                }
                case 6:{
                    printSellProductList();
                    break;
                }
                case 7:{
                    exit=true;
                    break;
                }
                case -1:{
                    System.out.println("Please write a number");
                    break;
                }
                default:{
                    System.out.println("Valid rank id: 1-7");
                }
            }
        }
    }

    public void createSellProduct()
    {
        String name=getNameUI();
        SellProduct sellProduct=sellControl.getSellProductByName(name);
        if(sellProduct==null){

            System.out.println("~Creating started~");
           //get info abou product
           int noOfOneType=getnoOfOneTypeUI();
           String description=getDescriptionUI();
           double price=getPriceUI();
           double discount=getDiscountUI();

           //store info to product....-> to arrayList
           sellProduct=sellControl.createSellProduct(name, description, noOfOneType, price,discount);
        }
        else{
            System.out.println("Same product was found!");
            int noToAdd=getnoOfOneTypeUI();
            int noAlreadyStored=sellProduct.getNoOfOneType();

            int together=noAlreadyStored+noToAdd;
            sellProduct.setNoOfOneType(together);
        }

        //print noOfType after added
        System.out.println("Now goods of this type in stok: "+sellProduct.getNoOfOneType());
        System.out.println("product for sale successful added");
    }

    protected SellProduct getSellProductByIDui()
    {

        SellProduct sellProduct;
        do{
            long idOfSaleProduct=getIdOfProductUI();
            sellProduct=sellControl.findSellProduct(idOfSaleProduct);
            if(sellProduct==null){
                System.out.println("SellProduct wasn't found");
                System.out.println("try another id? (y/n):");
                if(listener.getLineBoolean()==false) break;
            }
        }while(sellProduct==null);
        return sellProduct;

    }

    public void printSellProductInfo()
    {
        try{
            SellProduct sellProduct=getSellProductByIDui();
            sellProduct.printProductInfo();
        }
        catch(Exception e){
        }
    }

    public void printSellProductList()
    {
        sellControl.printSellProductList();
    }

    public void updateSellProduct()
    {
        boolean exit=false;
        SellProduct sellProduct=getSellProductByIDui();
        do{
            if(sellProduct==null) break;
            System.out.println("what you need to change?: ");
            printChoiceOfChanges();
            int line=listener.getLineInt();
            switch(line){
                case 1:{
                    String name=getNameUI();
                    sellProduct.setName(name);
                    break;
                }
                case 2:{
                    double price=getPriceUI();
                    sellProduct.setPrice(price);
                    break;
                }
                case 3:{
                    double discount=getDiscountUI();
                    sellProduct.setDiscount(discount);
                    break;
                }
                case 4:{
                    int noOfOneType=getnoOfOneTypeUI();
                    sellProduct.setNoOfOneType(noOfOneType);
                    break;
                }
                case 5:{
                    String description=getDescriptionUI();
                    sellProduct.setDescription(description);
                    break;
                }
                case 6:{
                    exit=true;
                    break;
                }
            }

        }while(exit==false);

    }


    public void printChoiceOfChanges()
    {
        System.out.println("\n(1) name: "+
                "\n(2) price: "+
                "\n(3) discount: "+
                "\n(4) amount of same kind"+
                "\n(5) description"+
                "\n(6) exit"
                );
    }

    public void deleteSellProduct()
    {
        SellProduct sellProduct= getSellProductByIDui();
        if(sellProduct!=null){
            long idOfProduct=sellProduct.getId();
            sellControl.deleteSellProduct(idOfProduct);
        }
    }

    public String getNameUI()
    {
        System.out.println("name: ");
        String name=listener.getLineString();
        return name;
    }

    public double getPriceUI()
    {
        System.out.println("price: ");
        double price=listener.getLineDouble();
        return price;
    }

    public double getDiscountUI()
    {
        System.out.println("discount: ");
        double discount=listener.getLineDouble();
        return discount;
    }

    public int getnoOfOneTypeUI()
    {
        System.out.println("amount of same kind: ");
        int noOfOneType=listener.getLineInt();
        return noOfOneType;
    }

    public String getDescriptionUI()
    {
        System.out.println("description: ");
        String description=listener.getLineString();
        return description;
    }

    public long getIdOfProductUI()
    {
        System.out.println("write ID of the sellProduct: ");
        long idOfProduct=listener.getLineInt();
        return idOfProduct;
    }
}
